/* This file is part of xREL
 *
 * Copyright (C) 2012 Timo Zimmermann <xrel@timozimmermann.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

import QtQuick 1.1
import com.nokia.meego 1.0

import "delegates"
import "components"

Page {
    id: root

    property string name: "ProductInfoPage"
    property string id

    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: pageStack.pop()
        }
        ButtonRow {
            exclusive: false
            TabButton {
                text: "Show releases"
                onClicked: pageStack.push(Qt.resolvedUrl("ReleasesScenePage.qml"), { id: id, title: xmlProductInfoModel.get(0).title })
            }
        }
    }

    XmlListModel {
        id: xmlProductInfoModel
        source: "http://api.xrel.to/api/ext_info/info.xml?id=" + root.id
        query: "/ext_info"

        XmlRole { name: "type"; query: "type/string()" }
        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "title"; query: "title/string()" }
        XmlRole { name: "link_href"; query: "link_href/string()" }
        XmlRole { name: "genre"; query: "genre/string()" }
        XmlRole { name: "cover_url"; query: "cover_url/string()" }
        XmlRole { name: "alt_title"; query: "alt_title/string()" }
        XmlRole { name: "rating"; query: "rating/number()" }
        XmlRole { name: "num_ratings"; query: "num_ratings/string()" }
    }

    XmlListModel {
        id: xmlProductInfoReleaseDatesModel
        source: "http://api.xrel.to/api/ext_info/info.xml?id=" + root.id
        query: "/ext_info/release_dates/date"

        XmlRole { name: "type"; query: "type/string()" }
        XmlRole { name: "date"; query: "date/string()" }
    }

    XmlListModel {
        id: xmlProductInfoExternalsModel
        source: "http://api.xrel.to/api/ext_info/info.xml?id=" + root.id
        query: "/ext_info/externals/external"

        XmlRole { name: "source_id"; query: "source/id/string()" }
        XmlRole { name: "source_name"; query: "source/name/string()" }
        XmlRole { name: "link_url"; query: "link_url/string()" }
        XmlRole { name: "plot"; query: "plot/string()" }
    }

    PageBackground { }

    PageHeader {
        id: pageHeader
        anchors.top: parent.top
        text: qsTr("Product Info")
    }

    Flickable {
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        contentHeight: contentColumn.height

        clip: true

        XmlViewBusyIndicator {
            xmlModel: undefined
            opacity: xmlProductInfoModel.status === XmlListModel.Loading ||
                     xmlProductInfoReleaseDatesModel.status === XmlListModel.Loading ||
                     xmlProductInfoExternalsModel.status === XmlListModel.Loading ? 0.5 : 0.0
        }

        Column {
            id: contentColumn
            width: parent.width

            ListView {
                id: productListView
                width: parent.width
                interactive: false
                cacheBuffer: 1

                delegate: ProductInfoDelegate { }
                model: xmlProductInfoModel
            }

            SectionLabel { text: qsTr("Release dates"); visible: releaseDatesListView.count > 0 }
            ListView {
                id: releaseDatesListView
                width: parent.width
                interactive: false
                cacheBuffer: 1

                delegate: ReleaseDatesDelegate { }
                model: xmlProductInfoReleaseDatesModel
            }

            SectionLabel { text: qsTr("External info"); visible: productExternalsInfoListView.count > 0 }
            ListView {
                id: productExternalsInfoListView
                width: parent.width
                interactive: false
                cacheBuffer: 1

                delegate: ExternalsInfoDelegate { }
                model: xmlProductInfoExternalsModel
            }
        }
    }
}
